import _ from 'lodash';
import { updatePack } from '../services/task';
import { user as getUser } from '../utils/user';

export default {
  namespace: 'examine',
  state: {
  },
  effects: {
    * startExamine({ payload: { value, onEnd } }) {
      try {
        const item = _.cloneDeep(value);
        const user = yield getUser();
        const examinerId = _.get(user, 'id');

        item.examineState = 'doing';
        item.examiner = examinerId;
        item.examineStartTime = Number(new Date());
        const id = item.id;
        delete item.id;

        yield updatePack({
          package: id,
          info: item,
        });
      } catch (e) {
        return onEnd(e);
      }
      return onEnd();
    },
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },
};
